package com.doucat.support.fast.table;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.doucat.support.component.fragment.host.ErrorFragment;
import com.doucat.support.component.fragment.host.HostActivity;
import com.doucat.support.component.recycle.adapter.CommonRecyclerAdapter;
import com.doucat.support.component.recycle.adapter.ItemClickListener;
import com.doucat.support.component.recycle.holder.BaseRecyclerHolder;
import com.doucat.support.component.start.StarterBuild;
import com.doucat.support.constant.PublicBundleConstant;
import com.doucat.support.web.WebActivity;
import com.tonly.doucatlibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * -----------------------------------------------------------------
 * Copyright (C) 2021-2031, by tonly, All rights reserved.
 * -----------------------------------------------------------------
 * File: TableAdapter.java
 * Author: wangfy
 * Create: 2021/6/3 10:24
 */
public class TableAdapter extends CommonRecyclerAdapter<WorkGroup> {
    private int publicSpanCount=5;

    public TableAdapter(Context context, List<WorkGroup> data) {
        super(context, data);
    }

    @Override
    public void onBindItemViewHolder(BaseRecyclerHolder holder, int position) {
        WorkGroup workGroup=getItem(position);
        TextView tvWorkClass= holder.getView(R.id.tvWorkClass);
        RecyclerView lvWork= holder.getView(R.id.lvWork);
        tvWorkClass.setText(workGroup.name);

        GridLayoutManager layoutManager = new GridLayoutManager(context,publicSpanCount);
        lvWork.setLayoutManager(layoutManager);
        List<WorkItem> workItems=new ArrayList<>();
        for(int i=0;i<workGroup.list.size();i++){
            WorkItem workItem=workGroup.list.get(i);
            if(workItem.enable){
                workItems.add(workItem);
            }
        }
        WorkAdapter adapter=new WorkAdapter(context, workItems);
        lvWork.setAdapter(adapter);
        adapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                WorkItem workItem=adapter.getItem(position);
                View.OnClickListener clickListener=workItem.onClickListener;
                if(clickListener==null){
                    //默认事件
                    if(workItem.type.equals(WorkItem.ACTIVITY)){
                        Intent intent=new Intent();
                        intent.setClassName(context.getPackageName(),workItem.target);
                        context.startActivity(intent);
                    }else if(workItem.type.equals(WorkItem.URL)){
                        Intent intent=new Intent(context, WebActivity.class);
                        intent.putExtra(PublicBundleConstant.URL,workItem.target);
                        context.startActivity(intent);
                    }
                    else if(workItem.type.equals(WorkItem.LOCAL_HTML)){
                        Intent intent=new Intent(context, HostActivity.class);
                        intent.putExtra(PublicBundleConstant.URL, WebActivity.class.getName());
                        intent.putExtra(PublicBundleConstant.URL_TITLE,"同力");
                        context.startActivity(intent);
                    }
                    else if(workItem.type.equals(WorkItem.FRAGMENT)){
                        new StarterBuild()
                                .setContext(context)
                                .setType(StarterBuild.START_HOST_ACTIVITY)
                                .setBundle(new Bundle())
                                .create()
                                .start(loadFragment(workItem.target).getClass());
                    }
                    else if(workItem.type.equals(WorkItem.NOT_DEFINE)){
                        Toast.makeText(context,"NOT DEFINE",Toast.LENGTH_SHORT).show();
                    }
                }else{
                    clickListener.onClick(view);
                }
            }
        });
    }
    public Fragment loadFragment(String className){
        Fragment fragment = new ErrorFragment(className);
        try {
            Class<?> fragClass = Class.forName(className);
            Object obj = fragClass.newInstance();
            fragment = (Fragment)obj;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return fragment;
    }
    @Override
    protected int getLayoutRes() {
        return R.layout.item_work_table;
    }
}